package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.client.LoginUserClient;
import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.LoginUser;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.dto.TenantEnteringDto;
import cn.itsource.hrm.exception.ErrorCode;
import cn.itsource.hrm.exception.GlobalException;
import cn.itsource.hrm.exception.ValidUtils;
import cn.itsource.hrm.mapper.EmployeeMapper;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.query.TenantQuery;
import cn.itsource.hrm.service.ITenantService;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.PageList;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2021-01-31
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private LoginUserClient loginUserClient;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public PageList<Tenant> selectPageList(TenantQuery query) {
        //分页条件，查询出来总数
        Page<Tenant> page = new Page<>(query.getPage(),query.getRows());
        //跨表查询出来结构
        List<Tenant> datas =  tenantMapper.loadPageList(page,query);
        return new PageList<>(page.getTotal(),datas);
    }

    @Override
    public void entering(TenantEnteringDto dto) {

        String password = dto.getEmployee().getPassword();
        String comfirmPwd = dto.getComfirmPwd();
        ValidUtils.assertEqual(password, comfirmPwd, ErrorCode.CODE_400002_PWD_EQUAL);

        //1 自定义错误
        //问题1：很多if判断错误
        //        if (StringUtils.isBlank(dto.getTenant().getCompanyName())){
        //            throw new GlobalException("机构名不能空！");
        //        }
        // ValidUtils.assertNotNull(dto.getTenant().getCompanyName(), "机构名不能空！");
        // 做正常业务逻辑之前要做校验
        //远程调用auth服务保存登录用户信息和套餐信息并返回login_id--LoginUser(登录用户信息 套餐)  //t_login_user (登录) t_user_meal
        LoginUser loginUser = new LoginUser();
        Employee employee = dto.getEmployee();
        loginUser.setUsername(employee.getUsername());
        loginUser.setPassword(employee.getPassword());
        loginUser.setType(0);
        loginUser.setMealId(dto.getMealId());
        AjaxResult ajaxResult = loginUserClient.addOrUpdate(loginUser);
        if (ajaxResult.isSuccess()) {
            Long longUserId = Long.valueOf( ajaxResult.getResultObj().toString());
            //保存管理员信息 login_id //t_employee
            employee.setRealName(employee.getUsername());
            employee.setInputTime(new Date());
            employee.setState(0);
            employee.setType(5);
            employee.setLoginId(longUserId);
            employeeMapper.insert(employee);
            //保存租户信息 admin_id //T_tenant
            Tenant tenant = dto.getTenant();
            tenant.setRegisterTime(new Date());
            tenant.setState(0);
            tenant.setAdminId(employee.getId());
            tenantMapper.insert(tenant);

            //private Long tenantId;
            employee.setTenantId(tenant.getId());
            employeeMapper.updateById(employee);
        }

    }

    @Override
    public Map<String, Object> getEmpAndTenant(Long loginUserId) {
        return tenantMapper.loadByLoginId(loginUserId);
    }
}
